﻿#include "../main/sound_spectrogram.h"

#include "lvgl.h"


#define SCREEN_WIDTH  224
#define SCREEN_HEIGHT 480

#define COLS          12
#define ROWS          12
#define BLOCK_W       16
#define BLOCK_H       10
#define BLOCK_GAP     2

static lv_obj_t* blocks[COLS][ROWS];

static lv_color_t gradient_green_to_orange(float ratio)
{
    uint8_t hue = 120 - (uint8_t)(ratio * 80);
    return lv_color_hsv_to_rgb(hue, 255, 100);
}

extern const lv_img_dsc_t renesas_logo;

static void create_ui(void)
{
    lv_obj_clean(lv_scr_act());
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_white(), 0);

    /* 创建并显示图片 */
    lv_obj_t* img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, &renesas_logo);
    lv_obj_align(img, LV_ALIGN_TOP_LEFT, 0, 0);

    /* 顶部文字区域 */
    lv_obj_t* top = lv_obj_create(lv_scr_act());
    lv_obj_set_size(top, SCREEN_WIDTH, 100); // 增加高度以适应图片
    lv_obj_set_style_bg_color(top, lv_color_white(), 0);
    lv_obj_set_style_border_width(top, 0, 0);
    lv_obj_align(top, LV_ALIGN_TOP_MID, 0, renesas_logo.header.h + 10); // 向下移动以适应图片

    lv_obj_t* label1 = lv_label_create(top);
    lv_label_set_text(label1, "CPKCOR-RA8D1");
    lv_obj_set_style_text_font(label1, &lv_font_montserrat_18, 0);
    lv_obj_set_style_text_color(label1, lv_color_black(), 0);
    lv_obj_align(label1, LV_ALIGN_TOP_MID, 0, 10);

    lv_obj_t* label2 = lv_label_create(top);
    lv_label_set_text(label2, "G.729A RD");
    lv_obj_set_style_text_font(label2, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(label2, lv_color_black(), 0);
    lv_obj_align(label2, LV_ALIGN_TOP_MID, 0, 45);

    /* 长方体网格：底部抬升 20 px，避开 FPS 栏 */
    int total_w = COLS * (BLOCK_W + BLOCK_GAP) - BLOCK_GAP;
    int start_x = (SCREEN_WIDTH - total_w) / 2;
    int total_h = ROWS * (BLOCK_H + BLOCK_GAP) - BLOCK_GAP;
    int start_y = SCREEN_HEIGHT - total_h - 40; // 向下移动以适应图片和文字

    for (int col = 0; col < COLS; col++)
    {
        for (int row = 0; row < ROWS; row++)
        {
            blocks[col][row] = lv_obj_create(lv_scr_act());
            lv_obj_set_size(blocks[col][row], BLOCK_W, BLOCK_H);
            lv_obj_set_pos(blocks[col][row],
                           start_x + col * (BLOCK_W + BLOCK_GAP),
                           start_y + (ROWS - 1 - row) * (BLOCK_H + BLOCK_GAP));
            lv_obj_set_style_radius(blocks[col][row], 0, 0);
            lv_obj_set_style_border_width(blocks[col][row], 0, 0);
            lv_obj_set_style_bg_color(blocks[col][row], lv_color_hex(0x999999), LV_PART_MAIN); // 默认灰色
        }
    }
}

static void spectrum_timer_cb(lv_timer_t* timer)
{
    for (int col = 0; col < COLS; col++)
    {
        int level = 1 + rand() % (ROWS + 1);   // 1..ROWS+1

        for (int row = 0; row < ROWS; row++)
        {
            if (row < level)
            {
                float ratio = (float)row / (ROWS - 1);
                lv_color_t c = gradient_green_to_orange(ratio);
                lv_obj_set_style_bg_color(blocks[col][row], c, LV_PART_MAIN);
            }
            else
            {
                lv_color_t c = lv_color_hex(0x999999); // 灰色（未点亮）
                lv_obj_set_style_bg_color(blocks[col][row], c, LV_PART_MAIN);
            }
        }
    }
}

void sound_spectrogram_create_ui(void)
{
    create_ui();
    lv_timer_create(spectrum_timer_cb, 100, NULL);  // 10 fps
}
